import json

from ..credentials.credentials import Credential

class Config:
    def __init__(self, endpoint, accessKey, secretKey, protocol = 'HTTPS', method = 'POST'):
        self.endpoint = endpoint
        self.accessKey = accessKey
        self.secretKey = secretKey
        self.protocol = protocol
        self.method = method

class Client:
    def __init__(self, config):
        if config.endpoint == None:
            raise ValueError('Endpoint is required')
        if config.accessKey == None or config.secretKey == None:
            raise ValueError('AccessKey and SecretKey are required')
        self.config = config

    def do_request(self, request_config):
        url = f"{self.config.protocol}://{self.config.endpoint}/{request_config['url']}"
        headers = request_config.get('headers', {})
        data = request_config.get('data', {})
        params = request_config.get('params', {})

        ctapi_request = {
            'url': url,
            'method': self.config.method,
            'headers': headers,
            'data': data,
            'params': params,
        }

        cre = Credential(self.config.accessKey, self.config.secretKey)
        response = cre.do_ctapi_request(ctapi_request)

        if isinstance(response, ValueError):
            print(f"client do request err: {response}")
            return response

        print(f'client do request success: {response}')
        return response

